<script>
export default {
	data() {
		return {
			activeFilter: 'comprehensive',
			priceSort: '', // 可以是 'asc' 或 'desc' 或空字符串
			productList: [
				{
					id: 1,
					name: '猫咪逗笑激光笔自动逗猫玩具',
					price: 29.9,
					originalPrice: 39.9,
					sales: 3201,
					image: '/static/dog-toy.png',
					promotion: '买2送1',
					freeShipping: true
				},
				{
					id: 2,
					name: '狗狗耐咬玩具橡胶球磨牙玩具',
					price: 19.8,
					originalPrice: 29.9,
					sales: 2871,
					image: '/static/cat-toy.png',
					promotion: '满3减10',
					freeShipping: true
				},
				{
					id: 3,
					name: '猫抓板剑麻猫爪板猫玩具',
					price: 35.5,
					sales: 1893,
					image: '/static/cat-toy.png',
					freeShipping: true
				},
				{
					id: 4,
					name: '宠物智能陪伴球自动逗猫玩具',
					price: 89,
					sales: 1054,
					image: '/static/dog-toy.png',
					freeShipping: true
				}
			]
		}
	},
	onLoad(options) {
		// 可以根据传入的参数加载不同的数据
		this.loadProductData()
	},
	methods: {
		loadProductData() {
			// 这里可以调用API获取数据
			// this.$api.productApi.getList({
			//   categoryId: 'toy',
			//   sortBy: this.activeFilter,
			//   order: 'desc'
			// }).then(res => {
			//   if(res && res.data) {
			//     this.productList = res.data
			//   }
			// }).catch(err => {
			//   console.error('获取商品列表失败:', err)
			// })
		},
		goBack() {
			uni.navigateBack()
		},
		navigateToSearch() {
			uni.navigateTo({
				url: '/pages/search/index'
			})
		},
		setFilter(filter) {
			if (filter === 'price' && this.activeFilter === 'price') {
				// 如果已经是价格筛选，则切换排序方向
				if (this.priceSort === 'asc') {
					this.priceSort = 'desc'
				} else if (this.priceSort === 'desc') {
					this.priceSort = ''
				} else {
					this.priceSort = 'asc'
				}
			} else {
				this.activeFilter = filter
				if (filter !== 'price') {
					this.priceSort = ''
				} else if (this.priceSort === '') {
					this.priceSort = 'asc'
				}
			}
			// 根据筛选条件重新加载数据
			this.loadProductData()
		},
		navigateToDetail(id) {
			uni.navigateTo({
				url: '/pages/shop/product-detail?id=' + id
			})
		}
	}
}
</script>